package com.chat.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chat.system.entity.dto.ChatFriendDTO;
import com.chat.system.entity.dto.ChatUserDTO;
import com.chat.system.entity.po.ChatUserRelationship;
import com.chat.system.entity.po.LatelyChatSession;
import com.chat.system.mapper.ChatUserRelationshipMapper;
import com.chat.system.service.IChatUserRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.system.service.IChatUserService;
import com.chat.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czh
 * @since 2023-10-28
 */
@Service
public class ChatUserRelationshipServiceImpl extends ServiceImpl<ChatUserRelationshipMapper, ChatUserRelationship> implements IChatUserRelationshipService {

    @Autowired
    IChatUserService chatUserService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public List<ChatFriendDTO> getFriendList(String currentUserName) {
        QueryWrapper<ChatUserRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",currentUserName).or().eq("friend_name",currentUserName);
        final List<ChatUserRelationship> list = list(queryWrapper);

        List<ChatFriendDTO> friendList = new ArrayList<>(list.size());
        for (ChatUserRelationship chatUserRelationship : list) {
            final String currentFriendName = UserUtil.getCurrentFriendName(currentUserName, chatUserRelationship);
            friendList.add(new ChatFriendDTO(chatUserRelationship.getId(),currentFriendName));
        }

        return friendList;
    }
}
